/**
 * 应用全局状态模块
 */
import storage from '@/utils/storage'

const state = {
  // 侧边栏状态
  sidebar: {
    opened: storage.getItem('sidebarStatus') ? !!storage.getItem('sidebarStatus') : true,
    withoutAnimation: false
  },
  // 设备类型
  device: 'mobile',
  // 应用主题
  theme: storage.getItem('theme') || 'default',
  // 系统消息
  message: {
    show: false,
    type: 'info', // success, warning, error, info
    content: '',
    duration: 3000
  },
  // 全局加载状态
  loading: false,
  // 系统通知
  notifications: [
    {
      id: 1,
      content: '您有 3 个待办事项需要处理，1 个报销申请待审批'
    }
  ],
  // 全局搜索状态
  globalSearch: {
    visible: false
  }
}

const mutations = {
  // 切换侧边栏
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      storage.setItem('sidebarStatus', 1)
    } else {
      storage.setItem('sidebarStatus', 0)
    }
  },
  // 关闭侧边栏
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    storage.setItem('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  // 切换设备类型
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  // 设置主题
  SET_THEME: (state, theme) => {
    state.theme = theme
    storage.setItem('theme', theme)
  },
  // 显示消息提示
  SHOW_MESSAGE: (state, { type = 'info', content = '', duration = 3000 }) => {
    state.message = {
      show: true,
      type,
      content,
      duration
    }
  },
  // 隐藏消息提示
  HIDE_MESSAGE: (state) => {
    state.message.show = false
  },
  // 设置加载状态
  SET_LOADING: (state, status) => {
    state.loading = status
  },
  // 设置通知
  SET_NOTIFICATIONS: (state, notifications) => {
    state.notifications = notifications
  },
  // 添加通知
  ADD_NOTIFICATION: (state, notification) => {
    state.notifications.unshift(notification)
  },
  // 移除通知
  REMOVE_NOTIFICATION: (state, id) => {
    state.notifications = state.notifications.filter(item => item.id !== id)
  },
  // 设置全局搜索状态
  SET_GLOBAL_SEARCH: (state, visible) => {
    state.globalSearch.visible = visible
  }
}

const actions = {
  // 切换侧边栏
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  // 关闭侧边栏
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  // 切换设备类型
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  // 设置主题
  setTheme({ commit }, theme) {
    commit('SET_THEME', theme)
  },
  // 显示消息提示
  showMessage({ commit }, message) {
    commit('SHOW_MESSAGE', message)
    
    // 定时关闭消息
    setTimeout(() => {
      commit('HIDE_MESSAGE')
    }, message.duration || 3000)
  },
  // 隐藏消息提示
  hideMessage({ commit }) {
    commit('HIDE_MESSAGE')
  },
  // 设置加载状态
  setLoading({ commit }, status) {
    commit('SET_LOADING', status)
  },
  // 获取通知列表
  getNotifications({ commit }) {
    // 这里可以调用API获取通知列表
    commit('SET_NOTIFICATIONS', [
      {
        id: 1,
        content: '您有 3 个待办事项需要处理，1 个报销申请待审批'
      }
    ])
  },
  // 添加通知
  addNotification({ commit }, notification) {
    const id = Date.now()
    commit('ADD_NOTIFICATION', { id, ...notification })
    return id
  },
  // 移除通知
  removeNotification({ commit }, id) {
    commit('REMOVE_NOTIFICATION', id)
  },
  // 切换全局搜索
  toggleGlobalSearch({ commit }, visible) {
    commit('SET_GLOBAL_SEARCH', visible)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 